/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ihm_clarifiee.Presentation;

import ihm_clarifiee.Presentation.model_table.model_table_backlog;
import ihm_clarifiee.application.Tache;
import ihm_clarifiee.application.Theme;
import ihm_clarifiee.Control.CHistoire;
import ihm_clarifiee.Control.CBacklog;
import ihm_clarifiee.application.Histoire;
import ihm_clarifiee.ajouter_histoire;
import ihm_clarifiee.assoc_histoire_sprint;
import ihm_clarifiee.base_affichage_histoire;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.*;

import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 28001673
 */
public class PBacklog extends javax.swing.JPanel {

private static final int DISPOSE_ON_CLOSE = 0;
	//  private CBacklog controle;
    private model_table_backlog md;
    private CBacklog controle;
    private ArrayList<CHistoire> listChistoire;
    boolean first;
    
    public PBacklog() {  
        initComponents();
        md=new model_table_backlog();
        tbl_principal.setModel(md);
        initModel();  
    }
    
    
    
     public PBacklog(CBacklog c) {
    	 System.out.println("--------->Création d'un PBacklog");
    	 System.out.println("de projet de titre : "+c.getProjet().getTitre());
        controle = c;
        listChistoire = c.getListeCHistoires();
        initComponents();
        md=new model_table_backlog();
        tbl_principal.setModel(md);
        initModel();    
    }
     
    public CBacklog getControle(){return controle;}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initModel ()
    {
        tbl_principal.getColumnModel().getColumn(0).setMinWidth(20);
        tbl_principal.getColumnModel().getColumn(0).setPreferredWidth(150);
        tbl_principal.getColumnModel().getColumn(0).setMaxWidth(2000);
        tbl_principal.getColumnModel().getColumn(1).setMinWidth(30);
        tbl_principal.getColumnModel().getColumn(1).setPreferredWidth(100);
        tbl_principal.getColumnModel().getColumn(1).setMaxWidth(1500);
         tbl_principal.getColumnModel().getColumn(2).setMinWidth(100);
        tbl_principal.getColumnModel().getColumn(2).setPreferredWidth(250);
        tbl_principal.getColumnModel().getColumn(2).setMaxWidth(4000);

        tbl_principal.getColumnModel().getColumn(3).setMinWidth(100);
       tbl_principal.getColumnModel().getColumn(3).setPreferredWidth(500);
       tbl_principal.getColumnModel().getColumn(3).setMaxWidth(4000);
        tbl_principal.getColumnModel().getColumn(4).setMinWidth(100);
       tbl_principal.getColumnModel().getColumn(4).setPreferredWidth(500);
       tbl_principal.getColumnModel().getColumn(4).setMaxWidth(4000);
        tbl_principal.getColumnModel().getColumn(5).setMinWidth(100);
       tbl_principal.getColumnModel().getColumn(5).setPreferredWidth(500);
       tbl_principal.getColumnModel().getColumn(5).setMaxWidth(4000);
 
        tbl_principal.getColumnModel().getColumn(0).setResizable(true);
        tbl_principal.getColumnModel().getColumn(1).setResizable(true);
        tbl_principal.getColumnModel().getColumn(2).setResizable(true);        
        tbl_principal.getColumnModel().getColumn(3).setResizable(true);       
        tbl_principal.getColumnModel().getColumn(4).setResizable(true);        
        tbl_principal.getColumnModel().getColumn(5).setResizable(true);       
        
        tbl_principal.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        
        
 
     }

  
    private void initComponents() {
    	first = true;
    	
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_principal = new javax.swing.JTable();
        bt_ajouter = new javax.swing.JButton();
        bt_suppr = new javax.swing.JButton();
        bt_modifier = new javax.swing.JButton();
        bt_detail = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        bt_monter = new javax.swing.JButton();
        bt_descendre = new javax.swing.JButton();
        bt_assocSprint = new javax.swing.JButton();
        
           
        jScrollPane1.setViewportView(tbl_principal);
        
        bt_ajouter.setText("Ajouter");
        bt_ajouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_ajouterActionPerformed(evt);
            }
        });

        bt_suppr.setText("Suppr");
  
          bt_suppr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_supprActionPerformed(evt);
            }
        });
          
          
        bt_modifier.setText("Modifier");

        bt_detail.setText("Detail");
        
          bt_detail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_detailActionPerformed(evt);
            }
        });
            bt_modifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modifierActionPerformed(evt);
            }
        });
        
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Backlog");
        jLabel1.setMaximumSize(new java.awt.Dimension(46, 24));

        bt_monter.setText("Monter");
        bt_monter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_monterActionPerformed(evt);
            }
        });
    
        bt_descendre.setText("Descendre");
        bt_descendre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_descendreActionPerformed(evt);
            }
        });
        
        bt_assocSprint.setText("Associer Sprint");
        bt_assocSprint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_assocSprintActionPerformed(evt);
            }
        });
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bt_descendre, 98, 98, 98)
                            .addComponent(bt_suppr, 98, 98, 98)
                            .addComponent(bt_ajouter, 98, 98, 98)
                            .addComponent(bt_modifier, 98, 98, 98)
                            .addComponent(bt_detail, 98, 98, 98)
                            .addComponent(bt_monter, 98, 98, 98)
                            .addComponent(bt_assocSprint, 98, 98, 98)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bt_ajouter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_modifier)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_suppr)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_detail)
                        .addGap(23, 23, 23)
                        .addComponent(bt_monter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bt_descendre)
                        .addGap(23,23,23)
                        .addComponent(bt_assocSprint))
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
    }                      

    private void bt_ajouterActionPerformed(java.awt.event.ActionEvent evt) {                                           
    	ajouter_histoire ah = new ajouter_histoire(this);
    	ah.setSize(1200,500);
    	ah.setVisible(true);
    }                                          

    public void addHistoire(Histoire h){
    	String nom = h.getNom();
    	String duree= String.valueOf(h.getDuree());
    	String desc = h.getDescription();
    	String taches = h.getTaches();
    	String theme = h.getTheme().getNom();
    	int i_etat = h.getEtat();
    	String etat = "";
    	if (i_etat == 0){etat = "A faire";}
    	if (i_etat == 1){etat = "En cours";}
    	if (i_etat == 2){etat = "En test";}
    	if (i_etat == 3){etat = "Fait";}
    	ArrayList<Tache> list = h.getListTaches();
    	
    	CHistoire ch = new CHistoire(this.getControle(), nom,desc,Integer.valueOf(duree),new Theme(theme, " "), i_etat);
    	ch.setListeTaches(list);
    	
    	String [] t ={nom,duree,desc,taches,theme,etat};
        md.add(t);
        tbl_principal.repaint();
        controle.ajouterFin(ch);
        int l = md.getRowCount();
        update(l-1);
    }
    
    private void bt_descendreActionPerformed(java.awt.event.ActionEvent evt) {                                             
     
        int sel = ligneSelectionnee();
        if (sel < tbl_principal.getRowCount()-1 && sel>=0){
              controle.downPrioHistoire(sel);
              md.down(sel);
              tbl_principal.repaint();
        }
    }
    
      private void bt_supprActionPerformed(java.awt.event.ActionEvent evt) {                                             
      
        int sel = ligneSelectionnee();
        
        if (sel != -1) {
        	md.remove(sel);
            controle.removeHistoire(sel);
            tbl_principal.repaint();
        }
    }
    
    
    private void bt_monterActionPerformed(java.awt.event.ActionEvent evt) {                                             
        int sel = ligneSelectionnee();
        if (sel < tbl_principal.getRowCount() && sel>0){
            controle.upPrioHistoire(sel);
             md.up(sel);
            tbl_principal.repaint();
        }
    }         
                                        
    private void bt_detailActionPerformed(java.awt.event.ActionEvent evt) { 
        
        int sel = ligneSelectionnee();
       
        if (sel != -1) {
        	PHistoire PH = ((CHistoire)(((CBacklog)controle).getHistoire(sel))).getPresentation();
            
            base_affichage_histoire bf = new base_affichage_histoire(PH);
            
            
            bf.getMainPanel().add(PH);
            bf.getMainPanel().setLayout(new FlowLayout());
            bf.setVisible(true);
            bf.setResizable(true);
            bf.setTitle("D�tail histoire");
            
            bf.setSize(1200,600);
            PH.setVisible(true);
            bf.setVisible(true);
            
            PH.setEditable(false);
            
            if(first){
            	PH.restoreTaches();
            	first = false;
            }
        }
    }                                            

     private void bt_modifierActionPerformed(java.awt.event.ActionEvent evt) {                                             
        int sel = ligneSelectionnee();
        
        if (sel != -1) {
        	PHistoire PH = ((CHistoire)(controle.getHistoire(sel))).getPresentation();
            
            base_affichage_histoire bf = new base_affichage_histoire(PH, this, sel);
            
            bf.getMainPanel().add(PH);
            bf.getMainPanel().setLayout(new FlowLayout());
            bf.setVisible(true);
            bf.setResizable(true);
            bf.setTitle("Modifier histoire");
            
            bf.setSize(1200,600);
            PH.setVisible(true);
            bf.setVisible(true);
            
            PH.setEditable(true);
            if(first){
            	PH.restoreTaches();
            	first = false;
            }
        }
    }      
     
    private void bt_assocSprintActionPerformed(java.awt.event.ActionEvent evt){
    	int sel = ligneSelectionnee();
    	
    	if (sel != -1) {
    		Histoire h = controle.getHistoire(sel);
    		
    		javax.swing.JFrame jf = new javax.swing.JFrame();

    		assoc_histoire_sprint a = new assoc_histoire_sprint(h, jf);
    		jf.add(a);
    		jf.setVisible(true);
    	}
    }
    
    public int ligneSelectionnee(){
        return tbl_principal.getSelectedRow();
    }
    
    public void update(int sel){
        
        ((model_table_backlog)(tbl_principal.getModel())).update(sel, 
        		((CBacklog)controle).getHistoire(sel).getNom(), 
        		((CBacklog)controle).getHistoire(sel).getDescription(), 
        		((CBacklog)controle).getHistoire(sel).getDuree(), 
        		((CBacklog)controle).getHistoire(sel).getTaches(),
        		((CBacklog)controle).getHistoire(sel).getTheme().getNom(),
        		((CBacklog)controle).getHistoire(sel).getEtat()
        		);
        tbl_principal.repaint();
    }
    
    
    public void updateAffichage(){
    	int taille = md.getRowCount();
    	while (taille>0){   		 
            md.remove(taille-1);
//          controle.removeHistoire(taille-1);
            taille--;         
    	} 
    	
    	for (int i=0; i<controle.getListeHistoires().size(); i++) {
    		Histoire h = controle.getHistoire(i);
    		String etat = "";
    		int i_etat = h.getEtat();
        	if (i_etat == 0){etat = "A faire";}
        	else if (i_etat == 1){etat = "En cours";}
        	else if (i_etat == 2){etat = "En test";}
        	else if (i_etat == 3){etat = "Fait";}
        	String [] t ={h.getNom(), h.getDescription(), String.valueOf(h.getDuree()), h.getTaches(),h.getTheme().getNom(),etat};
            md.add(t);
            tbl_principal.repaint();
    	}
    }
    	
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton bt_ajouter;
    private javax.swing.JButton bt_descendre;
    private javax.swing.JButton bt_detail;
    private javax.swing.JButton bt_modifier;
    private javax.swing.JButton bt_monter;
    private javax.swing.JButton bt_suppr;
    private javax.swing.JButton bt_assocSprint;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbl_principal;
    // End of variables declaration                   

}
